
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

                // �����鼮��Ϣ�ṹ��
                typedef struct BookInfo {
                char bookName[100]; // ����
                char bookID[50];    // ���
                float price;        // ���
                int remaining;      // ʣ�౾��
                struct BookInfo* prev; // ָ��ǰһ���鼮��ָ��
                struct BookInfo* next; // ָ���һ���鼮��ָ��
            } BookInfo;

            // ������ļ�¼�ṹ��
            typedef struct BorrowRecord {
                char bookName[100]; // ���ĵ�����
                char borrowTime[20]; // ����ʱ��
                char returnTime[20]; // ����ʱ��
                float fineAmount;    // ������
                struct BorrowRecord* next; // ָ����һ�����ļ�¼��ָ��
            } BorrowRecord;

            // �����û���Ϣ�ṹ��
            typedef struct Node {
                char name[100];               // ����
                char ID[50];                  // ѧ��/����/����֤��
                BorrowRecord* borrowRecords;  // ָ����ļ�¼������ͷָ��
                float totalFineAmount;        // �ܷ�����
                struct Node* prev;            // ָ��ǰһ���û���ָ��
                struct Node* next;            // ָ���һ���û���ָ��
            } Node;

            // �������鼮�ڵ�
            BookInfo* createBookNode() {
                BookInfo* newBook = (BookInfo*)malloc(sizeof(BookInfo));
                if (newBook == NULL) {
                    printf("�ڴ����ʧ��\n");
                    exit(1);
                }
                printf("������������");
                scanf("%99s", newBook->bookName);
                printf("��������ţ�");
                scanf("%49s", newBook->bookID);
                printf("�������");
                scanf("%f", &newBook->price);
                newBook->remaining = 5; // ��ʼʣ������Ϊ5
                newBook->prev = NULL;
                newBook->next = NULL;
                return newBook;
            }

            // �����鼮�ڵ㵽˫������ͷ��
            void insertBookAtHead(BookInfo * *head, BookInfo * newBook) {
                if (*head == NULL) {
                    *head = newBook;
                }
                else {
                    newBook->next = *head;
                    (*head)->prev = newBook;
                    *head = newBook;
                }
            }

            // �������û��ڵ�
            Node* createUserNode() {
                Node* newNode = (Node*)malloc(sizeof(Node));
                if (newNode == NULL) {
                    printf("�ڴ����ʧ��\n");
                    exit(1);
                }
                printf("������������");
                scanf("%99s", newNode->name);
                printf("������ѧ��/����/����֤�ţ�");
                scanf("%49s", newNode->ID);
                newNode->borrowRecords = NULL;
                newNode->totalFineAmount = 0.0;
                newNode->prev = NULL;
                newNode->next = NULL;
                return newNode;
            }

            // �����û��ڵ㵽˫������ͷ��
            void insertUserAtHead(Node * *head, Node * newNode) {
                if (*head == NULL) {
                    *head = newNode;
                }
                else {
                    newNode->next = *head;
                    (*head)->prev = newNode;
                    *head = newNode;
                }
            }

            // ���ӽ��ļ�¼
            void addBorrowRecord(Node * user, BookInfo * book) {
                BorrowRecord* newRecord = (BorrowRecord*)malloc(sizeof(BorrowRecord));
                if (newRecord == NULL) {
                    printf("�ڴ����ʧ��\n");
                    exit(1);
                }
                strcpy(newRecord->bookName, book->bookName);
                printf("���������ʱ�䣺");
                scanf("%19s", newRecord->borrowTime);
                printf("�����뻹��ʱ�䣺");
                scanf("%19s", newRecord->returnTime);
                if (newRecord->returnTime - newRecord->borrowTime > 60) {
                    newRecord->fineAmount = (newRecord->returnTime - newRecord->borrowTime - 60) * 2;
                }
                else {
                    newRecord->fineAmount = 0;
                }

                newRecord->next = NULL;

                // ���¼�¼���ӵ�����ĩβ
                if (user->borrowRecords == NULL) {
                    user->borrowRecords = newRecord;
                }
                else {
                    BorrowRecord* current = user->borrowRecords;
                    while (current->next != NULL) {
                        current = current->next;
                    }
                    current->next = newRecord;
                }

                user->totalFineAmount += newRecord->fineAmount;

                // �����鼮ʣ������
                book->remaining--;
            }

            // �����鼮
            BookInfo* findBook(BookInfo * head, const char* bookName) {
                BookInfo* current = head;
                while (current != NULL) {
                    if (strcmp(current->bookName, bookName) == 0) {
                        return current;
                    }
                    current = current->next;
                }
                return NULL;
            }

            // �����û�
            Node* findUser(Node * head, const char* userName) {
                Node* current = head;
                while (current != NULL) {
                    if (strcmp(current->name, userName) == 0) {
                        return current;
                    }
                    current = current->next;
                }
                return NULL;
            }

            // ��ӡ�鼮��Ϣ����
            void printBooks(BookInfo * head) {
                BookInfo* current = head;
                while (current != NULL) {
                    printf("������%s����ţ�%s����%.2f��ʣ�౾����%d\n",
                        current->bookName, current->bookID, current->price, current->remaining);
                    current = current->next;
                }
            }

            // ��ӡ�û���Ϣ����
            void printUsers(Node * head) {
                Node* current = head;
                while (current != NULL) {
                    printf("������%s\n", current->name);
                    printf("ѧ��/����/����֤�ţ�%s\n", current->ID);
                    printf("���ļ�¼��\n");
                    BorrowRecord* record = current->borrowRecords;
                    while (record != NULL) {
                        printf("  ���ĵ�������%s������ʱ�䣺%s������ʱ�䣺%s�������%.2f\n",
                            record->bookName, record->borrowTime, record->returnTime, record->fineAmount);
                        record = record->next;
                    }
                    printf("�ܷ����%.2f\n", current->totalFineAmount);
                    printf("------------------------\n");
                    current = current->next;
                }
            }

            // �ͷ��鼮��Ϣ�����ڴ�
            void freeBooks(BookInfo * head) {
                BookInfo* current = head;
                while (current != NULL) {
                    BookInfo* temp = current;
                    current = current->next;
                    free(temp);
                }
            }

            // �ͷ��û���Ϣ�����ڴ�
            void freeUsers(Node * head) {
                Node* current = head;
                while (current != NULL) {
                    BorrowRecord* record = current->borrowRecords;
                    while (record != NULL) {
                        BorrowRecord* tempRecord = record;
                        record = record->next;
                        free(tempRecord);
                    }
                    Node* tempNode = current;
                    current = current->next;
                    free(tempNode);
                }
            }

            void caozuo() {
                BookInfo* bookHead = NULL;
                Node* userHead = NULL;
                int choice;

                while (1) {
                    printf("\n1. �����鼮��Ϣ\n");
                    printf("2. �����û���Ϣ\n");
                    printf("3. �û������鼮\n");
                    printf("4. ��ӡ�鼮��Ϣ\n");
                    printf("5. ��ӡ�û���Ϣ\n");
                    printf("6. �˳�\n");
                    printf("��ѡ�������");
                    scanf("%d", &choice);

                    switch (choice) {
                    case 1: {
                        BookInfo* newBook = createBookNode();
                        insertBookAtHead(&bookHead, newBook);
                        break;
                    }
                    case 2: {
                        Node* newUser = createUserNode();
                        insertUserAtHead(&userHead, newUser);
                        break;
                    }
                    case 3: {
                        if (bookHead == NULL || userHead == NULL) {
                            printf("�鼮���û���ϢΪ�գ����������鼮���û���\n");
                            break;
                        }
                        printf("������Ҫ���ĵ�������");
                        char bookName[100];
                        scanf("%99s", bookName);
                        BookInfo* book = findBook(bookHead, bookName);
                        if (book == NULL) {
                            printf("δ�ҵ�������%s\n", bookName);
                            break;
                        }
                        if (book->remaining <= 0) {
                            printf("�鼮��%s ����ʣ��ɽ��ĵı�����\n", bookName);
                            break;
                        }
                        printf("����������û���������");
                        char userName[100];
                        scanf("%99s", userName);
                        Node* user = findUser(userHead, userName);
                        if (user == NULL) {
                            printf("δ�ҵ��û���%s\n", userName);
                            break;
                        }
                        addBorrowRecord(user, book);
                        break;
                    }
                    case 4: {
                        printBooks(bookHead);
                        break;
                    }
                    case 5: {
                        printUsers(userHead);
                        break;
                    }
                    case 6: {
                        freeBooks(bookHead);
                        freeUsers(userHead);
                        exit(0);
                    }
                    default: {
                        printf("��Ч��ѡ�����������롣\n");
                    }
                    }
                }

           
            }